package dk.aiszone.lang.messer.autosource;

import dk.aiszone.lang.autosource.generatorobjects.interfaces.GeneratorObject;
import dk.aiszone.lang.autosource.generatorobjects.interfaces.SimpleBlockObject;

/**
 * Representation of an variable assignment.
 * 
 * @author Ais
 */
public class VariableAssignment extends GeneratorObject implements SimpleBlockObject {

    private final String name;
    private final SimpleBlockObject value;

    /**
     * Constructor for the <code>VariableAssignment</code> object.
     * 
     * @param name
     *            The name of the variable being assigned.
     * @param value
     *            The value assigned to the variable.
     */
    public VariableAssignment(String name, SimpleBlockObject value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public Object clone() {
        return new VariableAssignment(name, (SimpleBlockObject)value.clone());
    }

    @Override
    public SimpleBlockObject getChild(int index) {
        if (index == 0 && value != null) {
            return value;
        }
        throw new IndexOutOfBoundsException(index + " out of bounds");
    }

    @Override
    public int getChildCount() {
        return (value != null ? 1 : 0);
    }

    @Override
    public String toJava() {
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        if (value != null) {
            sb.append(" = ").append(value.toJava());
        }
        return sb.toString();
    }
}